package club.justwill.single.persistence.dao;

import club.justwill.single.persistence.jpo.Skus;
import club.justwill.single.persistence.support.BaseDao;
import org.springframework.beans.factory.config.ConfigurableBeanFactory;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Repository;

import javax.persistence.NoResultException;
import javax.persistence.NonUniqueResultException;
import javax.persistence.Query;
import java.util.List;

/**
 * Created by xiangnan on 15-12-27.
 */
@Repository
@Scope(ConfigurableBeanFactory.SCOPE_SINGLETON)
public class SkuDao extends BaseDao<Skus> {

    public List<Skus> getSkusByProjectId(long projectId) {
        Query query = em.createNamedQuery("Skus.getByProjectId").setParameter("projectId", projectId);
        return query.getResultList();
    }

    public Skus getBySkuId(long skusId) {
        Query query = em.createNamedQuery("Skus.getBySkusId").setParameter("skusId", skusId);
        try {
            return (Skus) query.getSingleResult();
        } catch (NonUniqueResultException e) {
            return (Skus) query.getResultList().get(0);
        } catch (NoResultException e) {
            return null;
        }
    }

    public List<Skus> searchOrdersByProjectId(long projectId) {
        Query query = em.createNamedQuery("Skus.searchOrdersByProjectId").setParameter("projectId",projectId);
        return query.getResultList();
    }

    public List<Skus> searchSkuListBuSkuIdList(List<Long> skuIdList){
        //Collection<Long> skuIds = map.values();
        Query query = em.createNamedQuery("Skus.searchSkuListBySkuIdList").setParameter("skuIdList",skuIdList);
        return query.getResultList();
    }
}
